import { defineStore } from 'pinia'
import { ref } from 'vue'
import { reqUserInfo } from '@/api/user'
import { ElMessage } from 'element-plus'
// import { userinfo } from '@/api/user/type'

// 用户信息
export const userStore = defineStore('user', () => {
  const username = ref('');
  const avatar = ref('');

  // 获取用户信息
  const getUserInfo = async () => {
    const res = await reqUserInfo()
    if (res.code === 200) {
      username.value = res.data.checkUser?.username as string
      avatar.value = res.data.checkUser?.avatar as string
    } else {
      ElMessage.error(res.data.message)
    }
  }

  // 模拟退出登录
  const logout = () => {
    localStorage.removeItem('TOKEN');
    username.value = '';
    avatar.value = ''
  }

  return {
    username,
    avatar,
    getUserInfo,
    logout
  }
})